package com.by.basicweb.jsonserializer;

import cn.hutool.core.util.PhoneUtil;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;

/**
 * 序列化手机号
 *
 * @author YanBingHao
 * @since 2023/12/29
 */
public class PhoneSuffixFourSerializable extends JsonSerializer<String> {

    @Override
    public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        if (StringUtils.isBlank(value)) {
            gen.writeString(StringUtils.EMPTY);
            return;
        }
        gen.writeString(PhoneUtil.isPhone(value) ? PhoneUtil.hideBefore(value).toString() : value);
    }
}
